package com.imohoo.libs.http;

import com.imohoo.libs.retrofit2.ssl.CertUtils;
import okhttp3.OkHttpClient;
import okhttp3.internal.Platform;

import javax.net.ssl.SSLContext;
import java.util.concurrent.TimeUnit;

/**
 * Created by zhaobo on 2016/8/10.
 */
public class OkHttpUtils {
    private static OkHttpUtils instance;
    private OkHttpClient.Builder httpBuilder;

    private Platform platform = Platform.get();

    private OkHttpUtils() {
        httpBuilder = new OkHttpClient.Builder();
    }

    public static OkHttpUtils getInstance() {
        if (instance == null) {
            synchronized (OkHttpUtils.class) {
                if (instance == null) {
                    instance = new OkHttpUtils();
                }
            }
        }
        return instance;
    }

    /**
     * https的全局自签名证书
     *
     * @param certStr 证书字符串
     * @return
     */
    public OkHttpUtils setCertificates(String certStr) {
//        SSLContext sslContext = CertUtils.getSSLContext(certStr);
//        if (sslContext != null) {
//            httpBuilder.sslSocketFactory(sslContext.getSocketFactory());
//        }
        return this;
    }

    /** 全局读取超时时间 */
    public OkHttpUtils setReadTimeOut(int readTimeOut) {
        httpBuilder.readTimeout(readTimeOut, TimeUnit.MILLISECONDS);
        return this;
    }

    /** 全局写入超时时间 */
    public OkHttpUtils setWriteTimeOut(int writeTimeout) {
        httpBuilder.writeTimeout(writeTimeout, TimeUnit.MILLISECONDS);
        return this;
    }

    /** 全局连接超时时间 */
    public OkHttpUtils setConnectTimeout(int connectTimeout) {
        httpBuilder.connectTimeout(connectTimeout, TimeUnit.MILLISECONDS);
        return this;
    }
}
